package com.leyou.auth.config;


import com.leyou.auth.intercepter.LogInInterceptor;
import com.leyou.auth.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
//配置是否开启登录拦截功能,默认为false
@ConditionalOnProperty(prefix = "ly.auth", name = "enable", havingValue = "true")
public class MvcConfig implements WebMvcConfigurer {

    @Autowired
    @Lazy
    private JwtUtils jwtUtils;
    @Bean
    public LogInInterceptor logInInterceptor() {
        return new LogInInterceptor(jwtUtils);
    }

    @Autowired
    private ClientProperties prop;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //添加拦截器
        InterceptorRegistration interceptor = registry.addInterceptor(logInInterceptor());

        //配置拦截路径
        List<String> includePathPatterns = prop.getIncludePathPatterns();
        if (!CollectionUtils.isEmpty(includePathPatterns)) {
            interceptor.addPathPatterns(includePathPatterns);
        }

        //不拦截路径
        List<String> excludePathPatterns = prop.getExcludePathPatterns();
        if (!CollectionUtils.isEmpty(excludePathPatterns)) {
            interceptor.excludePathPatterns(excludePathPatterns);
        }


    }
}
